<?php

class Database
{
	private $connectoid = false; //Used to track this specific database connection so we don't mess with the other connections, also can be used to determine the state of the connection
	private static $user = "eqemu";	//Username to access the Database
	private static $pass = "eqpassword";	//Password to access the Database
	private static $host = "localhost";		//IP address or hostname
	protected $db = "peq";	//Name of the Database we're accessing, can be changed if needed
	
	protected $benchmarks = Array();
	
	public function __construct() {
		if ($this->connectoid = mysql_connect(Database::$host, Database::$user, Database::$pass)) {
			mysql_select_db($this->db, $this->connectoid);
		}
	}
	
	public function __toString() {
		return "Database " . ($this->IsConnected() ? "" : "not ") . "connected";
	}
	
	public function IsConnected() {
		return ($this->connectoid ? true : false);
	}
	
	//test function, we don't want to be broadcasting our login info to everyone irl
	public function ReturnLoginInfo() {
		return "Host: " . Database::$host . "<br>\nUser: " . Database::$user . "<br>\nPass: " . Database::$pass . "<br>\nDB: " . $this->db;
	}
	
	public function Query($q) {
		$this->benchmarks[Query][result][start] = microtime(true);
		$res = mysql_query($q, $this->connectoid);	//results
		$this->benchmarks[Query][result][end] = microtime(true);
		$this->benchmarks[Query][row][start] = microtime(true);
		if ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {	//single row of data
			$this->benchmarks[Query][row][end] = microtime(true);
			$i = 0;	//iterator
			$ret = array();
			do {
				$ret[++$i] = $row;
			} while ($row = mysql_fetch_array($res, MYSQL_ASSOC));
			if (is_array($ret))	//Only return data in an array, otherwise it's an error
				return $ret;
		} else {
			return $this->GetErrorNum($this->connectoid);	//May want to change this later, especially if we get something to do error handling
		}
	}
	
	public function GetTableInfo($t) {
		mysql_select_db("information_schema");	//switch over so we can get the info
		$q = "SELECT COLUMN_NAME,ORDINAL_POSITION,COLUMN_DEFAULT FROM COLUMNS WHERE TABLE_SCHEMA='" . $this->db . "' AND TABLE_NAME='" . $t . "' ORDER BY ORDINAL_POSITION ASC";
		$ret = $this->Query($q);
		mysql_select_db($db, $this->connectoid);	//switch it back to the original db
		/*$ret = $this->Query("SELECT CREATE TABLE spells_new");*/	//why doesn't this work?
		return $ret;
	}
	
	public function GetBenchmarks() {
		$this->TotalBenchmarks();
		return $this->benchmarks;
	}
	
	private function TotalBenchmarks() {
		$this->benchmarks[Query][result][total] = $this->benchmarks[Query][result][end] - $this->benchmarks[Query][result][start];
		$this->benchmarks[Query][row][total] = $this->benchmarks[Query][row][end] - $this->benchmarks[Query][row][start];
		/*foreach($this->benchmarks as $key => $value) {
			foreach() {
				
			}
		}*/
	}
	
	protected function GetErrorNum($c) {
		return mysql_errno($c);
	}
	
	protected function GetErrorMsg($c) {
		return mysql_error($c);
	}
	
	public function __destruct() {
		if ($this->IsConnected())
			mysql_close($this->connectoid);
	}
}

?>